package com.qingcheng.controller;

import com.qingcheng.domain.CategoryDto;
import com.qingcheng.entity.PageResult;
import com.qingcheng.entity.Result;
import com.qingcheng.pojo.goods.Category;
import com.qingcheng.pojo.goods.TbCategory;
import com.qingcheng.service.CategoryService;
import com.qingcheng.service.TbCategoryService;
import com.qingcheng.util.PageUtil;
import com.qingcheng.util.YouthR;
import com.qingcheng.entity.PageResult;
import com.qingcheng.entity.Result;
import com.qingcheng.pojo.goods.*;
import com.qingcheng.service.CategoryService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.xml.ws.soap.Addressing;
import java.util.*;

@Slf4j
@RestController
@RequestMapping("/category")
@Api(tags = "TbCategoryController")
public class CategoryController {

    @Autowired
    private CategoryService categoryService;

    @Resource
    private TbCategoryService tbCategoryService;

    @ApiOperation(value = "根据商品类别查询模板")
    @GetMapping("/categoryQueryTem/{C3Id}")
    public  YouthR CategoryQueryTem(@PathVariable Integer C3Id){
        Integer temId = tbCategoryService.C3IdQueryTem(C3Id);
        return YouthR.ok().data("temId",temId);
    }

    @ApiOperation(value = "根据id获取信息")
    @GetMapping("/detail/{id}")
    public YouthR detail(@PathVariable Integer id) {

        TbCategory tbCategory = tbCategoryService.findById(id);

        return YouthR.ok().data("tbCategory",tbCategory);
    }

    @ApiOperation(value = "获取信息（list不分页）")
    @GetMapping("/list")
    public YouthR list() {
        List<Map>  list = tbCategoryService.queryAll();
        return YouthR.ok().data("list",list);
    }



    @ApiOperation(value = "获取信息（list分页）不带条件")
    @GetMapping("/listPages")
    public YouthR listPages(Integer pageNum,Integer pageSize) {
        PageUtil<TbCategory> pageUtils = tbCategoryService.findListAll(pageNum, pageSize);

        return YouthR.ok().data("pageUtils",pageUtils);
    }

    @ApiOperation(value = "获取信息（list分页）带条件")
    @PostMapping("pageQueryByAll/{pageNum}/{pageSize}")
    public YouthR pageQueryByAll(@PathVariable Integer pageNum,
                                 @PathVariable Integer pageSize,
                                 @RequestBody TbCategory tbCategory){
        PageUtil<TbCategory> pageUtils = tbCategoryService.pageQueryByAll(pageNum, pageSize, tbCategory);
        return YouthR.ok().data("pageUtils",pageUtils);
    }

    @ApiOperation(value = "获取信息（list分页）多表联查")
    @PostMapping("classifications/{pageNum}/{pageSize}")
    public YouthR classifications(@PathVariable Integer pageNum,
                                  @PathVariable Integer pageSize,
                                  @RequestBody TbCategory tbCategory){
        PageUtil<CategoryDto> pageUtils = tbCategoryService.queryCategoryList(pageNum, pageSize, tbCategory);
        return YouthR.ok().data("pageUtils",pageUtils);
    }

    @ApiOperation(value = "添加信息")
    @PostMapping("/addCategory")
    public YouthR addCategory(@RequestBody TbCategory tbCategory) {

        boolean b = tbCategoryService.saveTbCategory(tbCategory);
        if(b){
            return YouthR.ok();
        }
        return YouthR.error();
    }

    @ApiOperation(value = "修改信息")
    @PutMapping("/update")
    public YouthR update(@RequestBody TbCategory tbCategory) {
        boolean b = tbCategoryService.updateTbCategory(tbCategory);

        if(b){
            return YouthR.ok();
        }
        return YouthR.error();
    }

    @ApiOperation(value = "删除信息（主键）")
    @DeleteMapping("/delete/{id}")
    public YouthR delete(@PathVariable Integer id) {
        boolean b = tbCategoryService.deleteById(id);
        if(b){
            return YouthR.ok();
        }
        return YouthR.error();
    }

    @GetMapping("/findAll")
    public List<Category> findAll(){
        return categoryService.findAll();
    }

    @GetMapping("/findPage")
    public PageResult<Category> findPage(int page, int size){
        return categoryService.findPage(page, size);
    }

    @PostMapping("/findList")
    public List<Category> findList(@RequestBody Map<String,Object> searchMap){
        return categoryService.findList(searchMap);
    }

    @PostMapping("/findPage")
    public PageResult<Category> findPage(@RequestBody Map<String,Object> searchMap,int page, int size){
        return  categoryService.findPage(searchMap,page,size);
    }

    @GetMapping("/findById")
    public Category findById(Integer id){
        return categoryService.findById(id);
    }


    @PostMapping("/add")
    public Result add(@RequestBody Category category){
        categoryService.add(category);
        return new Result(200,"添加成功",null);
    }

    @GetMapping("/findAllMenus")
    public Result queryAllMenus(){
        List allMenus = categoryService.findAllMenus();
        return new Result(200,"查询成功",allMenus);
    }

    @GetMapping("/queryByParentId")
    public Result queryByParentId(List<Category> categories,Integer parentId){
        List<Map> byParentId = categoryService.findByParentId(categories, parentId);
        return new Result(200,"",byParentId);
    }

    @GetMapping("/findAllCategory")
    public Result findAllCategory(){
        return new Result(200,"查询成功",categoryService.findAllCategory());
    }

    @GetMapping("/returnMenus")
    public Result ReturnMenus(){
        List<Map> caregory=tbCategoryService.findCategoryTree();
        return new Result(200,"查询成功",caregory);
    }

    // 根据商品的分类随机查询出推荐的手机
    @GetMapping("/random")
    public List<Map> recommend(Integer category3){
        List<Map>  spu=tbCategoryService.recommend(category3);
        return spu;
    }

    // 商品详情表的推荐相关列表
    @GetMapping("/recommendlists")
    public List<TbCategory> recommendlists(String id){
        List<TbCategory> spu=tbCategoryService.lists(id);
        return spu;
    }

    //  查询出家用电器下七天销量最高的分类
    @GetMapping("/appliances")
    public HashMap<Map,List<TbSpu>> appliances(String parent_id){
        HashMap<Map,List<TbSpu>> spu=tbCategoryService.appliances(parent_id);
        return spu;
    }

    //  今日推荐
    @GetMapping("today")
    public List<Map> Today(){
        return tbCategoryService.Today();
    }

}
